﻿using System.ComponentModel.DataAnnotations;
using WeatherHistory.Shared.Constants;

namespace WeatherHistory.Shared.Validation;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public class WeatherConditionAttribute : ValidationAttribute
{
    public WeatherConditionAttribute(bool allowNull) : base("Weather condition has invalid value")
    {
        AllowNull = allowNull;
    }

    private bool AllowNull { get; }

    public override bool IsValid(object? value)
    {
        return value?.ToString() switch
        {
            WeatherCondition.Storm or WeatherCondition.Clear or WeatherCondition.Cloudy or WeatherCondition.Fog or WeatherCondition.Rain or WeatherCondition.Snow => true,
            null => AllowNull,
            _ => false
        };
    }
}